package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

//订单的业务层实现类
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    //提交购物车
    @Override
    public void submitOrder(Order order) {
        //获得对象属性
        User user = UserHolder.get();
        //获得该用户购物车信息
        List<Cart> cartList = cartService.findList();
        //判断购物车中是否有菜品/套餐
        if (cartList == null) {
            throw new CustomException("购物车中没有菜品/套餐,请添加后重试!");
        }
        //购物车中有菜品/套餐
        //根据地址id获得地址信息
        Address address = addressService.findById(order.getAddressId());
        //判断地址信息中有无地址
        if (address == null) {
            throw new CustomException("用户地址不存在,请添加后重试!");
        }
        //生成订单id(同雪花算法)
        Long orderId = IdWorker.getId();
        //设置订单总金额
        BigDecimal total = new BigDecimal(0);

        //添加订单详情对象信息
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            //拷贝数据
            BeanUtils.copyProperties(cart, orderDetail);
            //设置订单id
            orderDetail.setOrderId(orderId);
            //调用mapper
            orderDetailMapper.insert(orderDetail);
            //计算总金额
            total = total.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
        }
        //添加订单对象信息
        order.setId(orderId);
        order.setNumber(String.valueOf(orderId));
        order.setStatus(1);
        order.setUserId(user.getId());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setPayMethod(1);
        order.setAmount(total);
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        //保存
        orderMapper.insert(order);
        //清空购物车
        cartService.delete();
    }

    //查询历史订单
    @Override
    public Page<Order> findOldOrder(Integer pageNum, Integer pageSize) {
        //创建条件查询对象
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(Order::getUserId, UserHolder.get().getId());
        qw.orderByDesc(Order::getOrderTime);
        //调用mapper
        List<Order> orderList = orderMapper.selectList(qw);
        //遍历订单列表
        for (Order order : orderList) {
            //根据订单id获得订单详情信息
            LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
            //创建条件查询语句
            lqw.eq(OrderDetail::getOrderId, order.getId());
            //调用mapper
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(lqw);
            //将订单详情信息设置进订单中
            order.setOrderDetails(orderDetails);
        }
        //开启分页
        Page<Order> page = new Page<>(pageNum, pageNum);
        //调用mapper
        page = orderMapper.selectPage(page, qw);
        //将查询到的数据设置进分页对象中
        page.setRecords(orderList);
        //返回
        return page;
    }

    //管理端订单查询
    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize, String number, String beginTime, String endTime) {
        //创建条件查询对象
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.like(StringUtils.isNotEmpty(number), Order::getNumber, number);
        qw.between(StringUtils.isNotEmpty(beginTime), Order::getOrderTime, beginTime, endTime);
        //开启分页
        Page<Order> page = new Page<>();
        //调用mapper
        page = orderMapper.selectPage(page, qw);
        //返回
        return page;
    }
}